<template>
  <el-dialog v-model="dialogFormVisible" :title="(isModify ? '修改' : '新增') + '账号'">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="平台：" prop="type">
        <el-select v-model="form.type" placeholder="请选择" clearable>
          <el-option
            v-for="item in platformList"
            :key="`platform-select-${item.code}`"
            :label="item.label"
            :value="item.type"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="别名：" prop="name">
        <el-input v-model="form.name" autocomplete="off" style="width: 260px" clearable />
      </el-form-item>
      <el-form-item label="账号ID：" prop="access_id">
        <el-input v-model="form.access_id" autocomplete="off" style="width: 260px" clearable />
      </el-form-item>
      <el-form-item label="账号Key：" prop="access_key">
        <el-input v-model="form.access_key" autocomplete="off" style="width: 260px" clearable />
      </el-form-item>
      <el-form-item label="补充密钥：">
        <el-input v-model="form.access_secret" autocomplete="off" style="width: 260px" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="saveAccount">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import EnumPlatform from "@/config/EnumPlatform"
import StoreCloudAccount from "@/store/StoreCloudAccount"
export default {
  name: 'AccountForm',
  data () {
    return {
      dialogFormVisible: false,
      isModify: false,
      platformList: EnumPlatform.valueList(),
      form: {
        type: 1,
        name: '',
        access_id: '',
        access_key: '',
        access_secret: ''
      },
      rules: {
        type: [
          { required: true, message: '请选择平台', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入别名', trigger: 'blur' }
        ],
        access_id: [
          { required: true, message: '请输入账号ID', trigger: 'blur' }
        ],
        access_key: [
          { required: true, message: '请输入账号Key', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * 显示窗口.
     * @param isModify
     * @param account
     */
    show(isModify, account) {
      this.dialogFormVisible = true
      this.isModify = isModify
      if (isModify) {
        this.form = Object.assign({}, account)
      }
    },
    /**
     * 保存账号.
     */
    saveAccount() {
      this.$refs.ruleForm.validate((valid) => {
        if(valid) {
          const updateData = Object.assign(this.form, {})
          if(this.isModify) {
            const id = this.form.id
            delete updateData.id
            StoreCloudAccount.update(updateData, {id: id})
          } else {
            StoreCloudAccount.insert(updateData)
          }
          this.$emit('refresh', {})
          this.$message.success('保存成功')
          this.dialogFormVisible = false
        }
      })
    }
  },
  mounted() {
    StoreCloudAccount.init(this)
  }
}
</script>

<style scoped>

</style>